package web.service;

import web.mapper.UserMapper;
import web.pojo.User;

import org.apache.ibatis.javassist.bytecode.annotation.IntegerMemberValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserMapper mapper;

    // 查询分页数据
    public Map<String, Object> list(Integer page, Integer size) {

        int limit = size == null || size < 1 ? 10 : size;
        // 处理前端提交的页码数
        page = page == null || page < 1 ? 1 : page;
        // 根据页码数、限制量求偏移量
        int offset = (page - 1) * limit;

        List<User> users = mapper.find(offset, limit);
        // 查询总数
        Long count = mapper.count();
        Map<String, Object> map = new HashMap<>();
        map.put("users", users);
        map.put("count", count);

        return map;
    }

    public Long checkUsernameAndPassword(String username, String password) {
        if (!mapper.exists(username)) {

            // 账号不存在，返回-1
            return -1l;
        }

        Long id = mapper.findIdUsernameAndPassword(username, password);
        if (id == null || id < 1) {
            // 账号和密码不匹配 返回 -2
            return -2l;
        }

        if (mapper.isDeleted(id)) {
            // 账号已删除
            return -3l;
        }
        if (mapper.isDisabled(id)) {
            // 账号已被禁用
            return -4l;
        }
        return id;
    }

    // 用户注册
    public Integer register(String username, String name) {

        // 用户存在1时
        if (mapper.exists(username)) {
            return -1;
        }else {
            mapper.addUser(username, name);
            return 1;
        }
    }

    public boolean modifyUserNameAndPassword(Long id, String name, String password) {
        try {
            mapper.updateUserNameAndPassword(id, name, password);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public boolean disableUser(Long id) {
        try {
            mapper.disableUser(id);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean enableUser(Long id) {
        try {
            mapper.enableUser(id);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean deleteUser(Long id) {

        try {
            mapper.deleteUser(id);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    public Map<String, Object> getUserInfo(Long id) {

        HashMap<String, Object> map = new HashMap<>();

        if (!mapper.existsById(id)) {
            return null;
        }

        User user = mapper.userInfo(id);

        map.put("id", user.getId());
        map.put("disabled", user.getDisabled());
        map.put("deleted", user.getDeleted());
        map.put("name", user.getName());
        map.put("username", user.getUsername());

        return map;
    }
}
